package atm.example;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * author:yan
 * function:provide function for banks
 * date:2025-9-29
 *  *    3. 转账
 **/
public class Bank implements MidFunction{
    private List<User> userList = new LinkedList<>();
    private User localUser;
    public List<User> getUserList(){
        return userList;
    }
    public void deleteUser(User user){
        userList.remove(user);
        System.out.println("删除成功");
    }
    public static void main(String[] args) {
        //主程序入口
        Bank bank1 = new Bank();
        bank1.welcomePage();
    }
    public boolean checkIdCard(String card){
        for(User user: userList){
            for(Account account : user.getAccountList()){
                if(account.getIdCard() == card){
                    return false;
                }
            }
        }
        return true;
    }
    public void welcomePage(){
        System.out.println("----------欢迎来到ATM系统，您可以输入以下功能前的序号来使用：-------------");
        int funcId = 0;
        Scanner sc = new Scanner(System.in);
        while(true){
            int funcNums = 3;
            System.out.printf("当前系统内拥有客户共%d\n", userList.size());
            System.out.println("1.登录账号\n2.注册账号\n3.退出系统");
            funcId = sc.nextInt();
            sc.nextLine();
            if(funcId > funcNums || funcId < 1){
                System.out.printf("当前输入功能序号有误，请重新输入%d-%d的数字:\n", 1, funcNums);
                continue;
            }
            switch (funcId){
                case 1:{
                    if(userList.size() == 0){
                        System.out.println("当前系统无账号");
                        break;
                    }
                    if(loginPage()){
                        System.out.println("--------ATM系统----------");
                        localUser.userManagePage(this);
                    }else{
                        System.out.println("登录失败");
                    }
                    break;
                }
                case 2:{
                    addUser();
                    System.out.println("--------ATM系统----------");
                    break;
                }
                case 3:{
                    System.out.println("系统已退出，欢迎下次使用");
                    return;
                }
                default:break;
            }
        }
    }
    public boolean loginPage(){

        //循环判断用户名是否存在，存在后检查密码输入是否正确，使用User类提供的方法进行判断
        System.out.println("请输入您的用户名");
        Scanner sc = new Scanner(System.in);
        User user = null;
        Out:
        while(true) {
            String userName = sc.next();
            for(User temp : userList){
                if(temp.getUserName().equals(userName)){
                    user = temp;
                    break Out;
                }
            }
            System.out.println("没有该用户，请重新输入:");

        }
        System.out.println("请输入密码");
        int times = 3;
        while(times > 0){
            times--;
            String passwd= sc.next();
            if(user.checkUserPasswd(passwd)){
                localUser = user;
                return true;
            }
            System.out.printf("密码错误，请重新输入(您还有%d次机会)\n", times);
        }
        return false;
    }
    public void addUser() {
        //传递到User类的添加信息，避免密码的传播
        User user = new User();
        user.setInfo();
        userList.add(user);

    }

}
interface MidFunction{
    default boolean checkIdCard(String card){
        return false;
    }
    static void clearScreen() {
        System.out.print("\\033[H\\033[2J");
        System.out.flush();
    }
}
